//
//  CustomCollectionViewCell.m
//  atlas
//
//  Created by 你好 on 16/12/28.
//  Copyright © 2016年 xyxNav. All rights reserved.
//

#import "CustomCollectionViewCell.h"

@implementation CustomCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];

    if (self)
    {
        [self configUI];
    }
    
    return self;
}



-(void)configUI
{
    self.backImageView=[[UIImageView alloc]init];
    self.backImageView.contentMode=UIViewContentModeScaleAspectFill;
    self.backImageView.clipsToBounds=YES;
    [self.contentView addSubview:self.backImageView];
    
    self.backView=[[UIView alloc]init];
    self.backView.backgroundColor=RGBACOLOR(1, 1, 1, 0.2);
    [self.contentView addSubview:self.backView];
    
    self.centerLabel=[[UILabel alloc]init];
    self.centerLabel.numberOfLines=1;
    self.centerLabel.textAlignment=NSTextAlignmentCenter;
    self.centerLabel.backgroundColor=[UIColor clearColor];
    self.centerLabel.textColor=[UIColor colorWithWhite:1 alpha:0.9];
    [self.contentView addSubview:self.centerLabel];

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
    {
        self.centerLabel.font=[UIFont systemFontOfSize:18];
    }
    else
    {
        self.centerLabel.font=[UIFont systemFontOfSize:24];
    }

    
    self.coverView=[[UIView alloc]init];
    self.coverView.backgroundColor=[UIColor colorWithWhite:1 alpha:0.5];
    [self.backImageView addSubview:self.coverView];
    
    self.centerLabel.hidden=YES;
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.contentView);
    }];
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.contentView);
    }];
    
    [self.centerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.contentView);
        make.center.equalTo(self.contentView);
    }];
    
    [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.backImageView);
    }];
    
}


-(void)configData:(PhotoAlbum *)photoAlbum
{
    self.coverView.alpha=0.5f;
    
    NSString *bgImageUrl=[NSString stringWithFormat:@"%@/%@?token=%@",GETIMAGE_URL,photoAlbum.imageUrl,[UserManager shareInstance].userToken];
    self.centerLabel.text=photoAlbum.name;
    [self.backImageView sd_setImageWithURL:[NSURL URLWithString:bgImageUrl] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.centerLabel.hidden=NO;
        
        UIView *tempView=[[UIView alloc]init];
        tempView.backgroundColor=[UIColor clearColor];
        [self.contentView addSubview:tempView];
        
        [tempView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.bottom.equalTo(self.contentView);
        }];
        
        [UIView animateWithDuration:1.5 animations:^{
            self.coverView.alpha=0.02f;
        }completion:^(BOOL finished) {
            [tempView removeFromSuperview];
        }];
    }];
}


@end
